package com.hospital.service.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.project.model.param.*;
import com.hospital.service.project.model.vo.ProGroupPageInfoVo;
import com.hospital.service.publics.service.PubFilePathService;
import com.hospital.service.sample.model.param.SamInfoBaseParam;
import common.core.util.SystemLog;
import enhance.plus.logic.LogicBaseController;
import com.hospital.service.project.entity.ProGroupInfo;
import com.hospital.service.project.entity.help.ProGroupInfoHelp;
import com.hospital.service.project.service.ProGroupInfoService;
import com.hospital.service.project.model.vo.ProGroupInfoVo;
import common.core.util.CommonUtil;
import common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import enhance.plus.logic.LogicBaseController;
import enhance.plus.util.Page;
import enhance.plus.util.PageHelper;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 实验分组表 控制器
 *
 * @author lfb
 * @created 2020-10-19 09:02:05
 */
@Slf4j
@Validated
@RestController
@Api(tags = "实验分组表")
@RequestMapping("/project/proGroupInfo")
public class ProGroupInfoController extends LogicBaseController<ProGroupInfoService, ProGroupInfo> {

    @Autowired
    private ProGroupInfoService proGroupInfoService;

    @Autowired
    private PubFilePathService pubFilePathService;


    @ApiOperation(value = "实验分组编号生成")
    @RequestMapping(value = "/getGroupNumber" , method = RequestMethod.GET)
    public Result getGroupNumber(@Valid ProGroupInfoParam param){
        log.debug("开始生成实验分组编号-->{}",param);
        return Result.success(this.proGroupInfoService.getGroupNumber());
    }

    @ApiOperation(value = "保存实验分组表的数据")
    @SystemLog(value = "增加实验分组", logo = "groupNumber")
    @RequestMapping(value = "/save" , method = RequestMethod.PUT)
    public Result save(@Valid @RequestBody ProGroupInfoParam param){
        log.debug("开始保存实验分组表-->{}",param);
        this.proGroupInfoService.add(param);
        return Result.success();
    }

    @ApiOperation(value = "更新实验分组表的数据")
    @SystemLog(value = "更新实验分组", logo = "groupNumber")
    @RequestMapping(value = "/update" , method = RequestMethod.PUT)
    public Result update(@Valid @RequestBody ProGroupInfoParam param){
        log.debug("开始跟新实验分组表-->{}",param);
        return Result.success(this.proGroupInfoService.updateById(CommonUtil.copyBean(param,ProGroupInfo.class)));
    }

    @ApiOperation(value = "删除实验分组表的一条数据")
    @RequestMapping(value = "/delete" , method = RequestMethod.DELETE)
    @ApiImplicitParams(
        @ApiImplicitParam(name= "id" ,value = "数据ID", required = true , example = "-1" )
    )
    public Result delete(@Valid @RequestParam @NotBlank(message = "删除数据ID不能为空") String id){
        log.debug("开始删除实验分组表的数据-->{}",id);
        return Result.success(this.proGroupInfoService.removeById(id));
    }

    @ApiOperation(value = "根据条件查询一条实验分组表的数据")
    @RequestMapping(value = "/queryForOneRow" , method = RequestMethod.POST)
    public Result<ProGroupInfoVo> queryForOneRow(@Valid @RequestBody ProGroupInfoQueryForOneRowParam param){
        ProGroupInfo row = this.proGroupInfoService.getOne(param.convertParamToQueryWapper());
        return Result.success( CommonUtil.copyBean(row, ProGroupInfoVo.class));
    }

    @ApiOperation(value = "根据条件查询实验分组表的数据-一对多 ： 一个实验分组对应多个实验样本数据")
    @RequestMapping(value = "/queryInfo" , method = RequestMethod.POST)
    public Result<ProGroupPageInfoVo> queryInfo(@Valid @RequestBody ProGroupInfoQueryPageParam param){
        return Result.success(this.proGroupInfoService.queryInfo(param));
    }

    @ApiOperation(value = "根据条件查询实验分组的数据 ： 查询条件  项目检索编号")
    @RequestMapping(value = "/queryDetail", method = RequestMethod.POST)
    public Result<List<ProGroupInfoVo>> queryDetail(@Valid @RequestBody ProGroupInfoQueryListParam param){
        QueryWrapper<ProGroupInfo> query = ProGroupInfoHelp.createQuery();
        query.eq(ProGroupInfoHelp.projectNumber,param.getProjectNumber());
        return Result.success(this.proGroupInfoService.list(query));
    }

    @ApiOperation(value = "实验分组导入")
    @PostMapping(value = "/groupImport")
    public Result groupImport(@RequestBody MultipartFile file) {
        this.proGroupInfoService.importGroup(file);
        return Result.success();
    }

    @ApiOperation(value = "实验分组导出")
    @PostMapping(value = "/groupExport")
    public Result groupExport(@Valid @RequestBody ProGroupInfoQueryListParam param
            , HttpServletResponse response) {
        this.proGroupInfoService.groupExport(param, response);
        return Result.success();
    }



}
